//
//  TMGCategoryListViewController.m
//  TMGold
//
//  Created by Mac on 2018/1/22.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGCategoryListViewController.h"
#import "TMGProductTableViewCell.h"
#import "TMGCategoryListTableViewCell.h"
#import "TMGNewBottomGoldDetailController.h"
#import "TMGNewSafePlanDetailController.h"
@interface TMGCategoryListViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation TMGCategoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.name;
    self.tableView.st_noreslutView  =  [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无该类商品" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header =  [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendGoodListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGCategoryListTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGCategoryListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGCategoryListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGProductModel * productModel = self.dataSouce[indexPath.row];
    /**
     商品id 1 新手特权金 2（口袋巾/流动金） 3保箱金  4零钱口袋  5安心计划  6 月月有息
     */
    if (self.model.productID.integerValue == 2) {
        TMGFlowViewController * vc = [TMGFlowViewController new];
        vc.model = productModel;
       [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.model.productID.integerValue == 3) {
        TMGNewBottomGoldDetailController * vc = [[TMGNewBottomGoldDetailController alloc] initWithStyle:UITableViewStyleGrouped];
        vc.model = productModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.model.productID.integerValue == 4) {
        TMGSmallPlanViewController * vc = [TMGSmallPlanViewController new];
        vc.model = productModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.model.productID.integerValue == 5) {
        TMGNewSafePlanDetailController * vc = [TMGNewSafePlanDetailController new];
        vc.model = productModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (self.model.productID.integerValue == 6) {
        TMGMonthPlanViewController * vc = [TMGMonthPlanViewController new];
        vc.model = productModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)sendGoodListRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/goods/getGoods"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.model.productID.description.length) {
         [dic setObject:self.model.productID forKey:@"cate"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array =  responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 
                                                 for (NSDictionary * modelDic in array) {
                                                     TMGProductModel * model = [TMGProductModel mj_objectWithKeyValues:modelDic];
                                                     model.desString = @"稳定生息 省心省力";
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}


@end
